// 请求根路径，一般是域名
const base_url = "https://XXXX/api"  

/*
示例1.数据请求
this.$api.requests({
	url: '/users',
	method: 'get',
	data: {
		id: this.id,
		type:''
	}
}).then(res => {
	console.log('res', res)
}).catch(err => {
	console.log('err', err)
})

示例2.formData文件上传
// 发送图片
this.$api.uploadfile({
	url: '/uploadFile/',
	filePath:path,
	name:'img',
	formData: {
		id: this.id
	}
}).then(res => {
	console.log('res', res)
}).catch(err => {
	console.log('err', err)
})

 */

const uploadfile = (options, errorlog = true, mask = true) => {
	return new Promise((resolve, reject) => {
		if (mask) {
			uni.showLoading({
				title: '上传中',
				mask: true
			});
		}
		const uploadTask = uni.uploadFile({
			url: base_url + options.url,
			filePath: options.filePath,
			name: options.name,
			formData: options.formData,
			header: {
				'token': uni.getStorageSync("token")  
			},
			success: (res) => {
				if (res.statusCode < 300) {
					resolve(JSON.parse(res.data))
				} else {
					if (res.statusCode == 401) {
						uni.showModal({
							title: '需要登录',
							content: "此功能需要登录使用哦",
							showCancel: false,
							confirmText: '去登录',
							success: function(res) {
								uni.redirectTo({
									url: '/pages/user/login.vue'
								});
							}
						})
					} else if (errorlog) {
						let str = ""
						let resdata = JSON.parse(res.data)
						for (let key in resdata) {
							if (typeof(resdata[key]) == "string") {
								str += resdata[key] + '\n'
							} else {
								for (let k in resdata[key]) {
									str += resdata[key][k] + '\n'
								}
							}
						}

						uni.showModal({
							title: '一不小心出错啦',
							content: str,
							showCancel: false,
							confirmText: '知道了'
						})

					}
					reject(false)
				}
			},
			fail: (err) => {
				uni.showToast({
					icon: 'none',
					title: '不小心送丢了~重试试吧',
					duration: 10000
				})
				reject(err)
			},
			complete: () => {
				if (mask) {
					uni.hideLoading()
				}
			}
		});
		// uploadTask.onProgressUpdate(function(res) {
		// 	options._this.percent = res.progress;
		// 	console.log('上传进度' + res.progress);
		// 	console.log('已经上传的数据长度' + res.totalBytesSent);
		// 	console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
		// });
	})
}
const requests = (_options, errorlog = true, mask = true, redirect = null) => {
	return new Promise((resolve, reject) => {
		if (mask) {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}
		let _data = {}
		let _header = {}
		if (uni.getStorageSync("token")) {
			_header = {
				'token': uni.getStorageSync("token")  
			}
		}
		if (_options.hasOwnProperty("data")) {
			_data = _options.data
		}
		if (_options.hasOwnProperty("header")) {
			_header = $.extend(header, _options.header)
		}
		uni.request({
			url: _options.url.indexOf("://") == -1 ? base_url + _options.url : _options.url,
			method: _options.method,
			data: _data,
			header: _header,
			success: (res) => {
				if (res.statusCode >= 500) {
					return
				}
				if (res.statusCode < 300) {
					resolve(res.data)
				} else {
					if (res.statusCode == 401) {
						uni.showModal({
							title: '需要登录',
							content: "此功能需要登录使用哦",
							showCancel: false,
							confirmText: '去登录',
							success: function(res) {
								uni.redirectTo({
									url: '/pages/user/login.vue'
								});
							}
						})
					} else if (errorlog) {
						let str = ""
						for (let key in res.data) {
							str += res.data[key] + '\n'
						}

						uni.showModal({
							title: `错误`,
							content: str,
							showCancel: false,
							confirmText: '知道了',
							success: function() {
								if (redirect) {
									uni.reLaunch({
										url: redirect
									})
								}
							}
						})
					}
				}
			},
			fail: (err) => {
				if (mask) {
					uni.showToast({
						icon: 'none',
						title: '数据跑丢了~重试试吧',
						duration: 10000
					})
				}
				reject(err)
			},
			complete: () => {
				if (mask) {
					uni.hideLoading()
				}
			}
		});
	})
}
module.exports = {
	requests: requests,
	uploadfile: uploadfile,
}
